package demo1.dao.sql;

import demo1.dao.Mapper.userMaiMapper;

import java.sql.*;

public class userMaisql implements userMaiMapper {

    @Override
    public ResultSet selectAll(String userName, String userPwd)throws Exception {
        Connection conn = conn();

        String sql = "SELECT * FROM usersmai WHERE userName = ? and userPwd = ?";
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //设置？的值
        try {
            pstmt.setString(1, userName);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        try {
            pstmt.setString(2, userPwd);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //执行sql
        ResultSet rs = null;
        try {
            rs = pstmt.executeQuery();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return rs;
    }

    @Override
    public void updateUserMaiName(String userName,Integer id) throws Exception{
        //1.注册驱动
        Connection conn = conn();
        String sql = "update usersmai set userName = (?) where id = (?)";
        //获取pstmt对象
        PreparedStatement pstmt = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, userName);
            pstmt.setInt(2, id);
            pstmt.executeUpdate();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateUserMaiAddress(String userAddress,Integer id) throws Exception{
        Connection conn = conn();
        String sql2 = "update usersmai set userAddress = (?) where id = (?)";
        //获取pstmt对象
        PreparedStatement pstmt2 = null;
        try {
            pstmt2 = conn.prepareStatement(sql2);
            pstmt2.setString(1, userAddress);
            pstmt2.setInt(2, id);
            pstmt2.executeUpdate();
            pstmt2.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateUserMaiDo(String userDo,Integer id) throws Exception{
        Connection conn = conn();
        String sql3 = "update usersmai set userDo = (?) where id = (?)";
        //获取pstmt对象
        PreparedStatement pstmt3 = null;
        try {
            pstmt3 = conn.prepareStatement(sql3);
            pstmt3.setString(1, userDo);
            pstmt3.setInt(2, id);
            pstmt3.executeUpdate();
            pstmt3.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updateUserMaiPwd(String userPwd, Integer id) throws Exception{
        Connection conn = conn();
        String sql3 = "update usersmai set userPwd = (?) where id = (?)";
        //获取pstmt对象
        PreparedStatement pstmt3 = null;
        try {
            pstmt3 = conn.prepareStatement(sql3);
            pstmt3.setString(1, userPwd);
            pstmt3.setInt(2, id);
            pstmt3.executeUpdate();
            pstmt3.close();
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertUserMai(String userName, String userPwd) throws Exception {
        Connection conn = conn();
        String sql = "INSERT INTO usersmai (userName, userPwd) VALUES (?, ?)";
        //获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);

        //设置？的值
        pstmt.setString(1, userName);
        pstmt.setString(2, userPwd);
        //执行sql
        pstmt.executeUpdate();
        pstmt.close();
        conn.close();
    }

    @Override
    public ResultSet selectAll() throws Exception {
        Connection conn = conn();
        String sql = "select * from usersmai";
        //获取pstmt对象
        PreparedStatement pstmt = conn.prepareStatement(sql);
        //执行sql
        return pstmt.executeQuery();

    }
    public Connection conn()throws Exception{
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/project2";
        String user = "root";
        String password = "111214Cjj";

        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
}
